package app

import (
	"git.sr.ht/~mil/mobsql/sqlgen"
	"github.com/jmoiron/sqlx"
)

type MDB struct {
	DB     *sqlx.DB
	Schema sqlgen.LoadSchema
}

type SourcesetFilter struct {
	Mdbid        []int       `yaml:"mdbid,omitempty" json:"mdbid,omitempty"`               // Any of mdbid match
	Country      string      `yaml:"country,omitempty" json:"country,omitempty"`           // Country match
	Municipality string      `yaml:"municipality,omitempty" json:"municipality,omitempty"` // Municipality match
	Subdivision  string      `yaml:"subdivision,omitempty" json:"subdivision,omitempty"`   // Subdivision match
	Provider     string      `yaml:"provider,omitempty" json:"provider,omitempty"`         // Provider match
	Name         string      `yaml:"name,omitempty" json:"name,omitempty"`                 // Name match
	Status       string      `yaml:"status,omitempty" json:"status,omitempty"`             // Status match
	Glob         string      `yaml:"glob,omitempty" json:"glob,omitempty"`                 // Glob match (Country, Municipality, Subdivision, Provider, Name)
	Coords       [][]float64 `yaml:"coords,omitempty" json:"coords,omitempty"`             // Coordinate match (expected at [lat,lon]) within bounding box
	MaxKM        float64     `yaml:"maxkm,omitempty" json:"maxkm,omitempty"`               // Max kilometers match (e.g. bounding box must be smaller then)
	Loaded       *bool       `yaml:"loaded,omitempty" json:"loaded,omitempty"`             // Loaded match (e.g. true/false or nil for both loaded & not loaded)
	Computed     *bool       `yaml:"computed,omitempty" json:"computed,omitempty"`         // Computed match (e.g. true/false or nil for both computed & not computed)
}

type SourceStatus struct {
	Loaded               bool     `db:"loaded" json:"loaded"`                                   // Whether the source has been loaded (any tables)
	Computed             bool     `db:"computed" json:"computed"`                               // Whether the source has been computed (any tables)
	TablesLoaded         *string  `db:"tables_loaded" json:"tables_loaded ,omitempty"`          // Tables loaded
	TablesComputed       *string  `db:"tables_computed" json:"tables_computed ,omitempty"`      // Tables computed
	TimestampLoaded      *string  `db:"timestamp_loaded" json:"timestamp_loaded,omitempty"`     // Latest timestamp of loaded tables
	TimestampComputed    *string  `db:"timestamp_computed" json:"timestamp_computed,omitempty"` // Latest timestamp of computed tables
	Mdbid                int      `db:"mdbid" json:"mdbid"`                                     // Mobility Database field 'mdb_source_id'
	Url                  string   `db:"url" json:"url"`                                         // Mobility Database field 'urls.latest'
	Provider             string   `db:"provider" json:"provider"`                               // Mobility Database field 'provider'
	Name                 *string  `db:"name" json:"name,omitempty"`                             // Mobility Database field 'name'
	Status               string   `db:"status" json:"status,omitempty"`                         // Mobility Database field 'status'
	LocationSubdivision  *string  `db:"subdivision" json:"subdivision"`                         // Mobility Database field 'location.subdivision_name'
	LocationCountry      string   `db:"country" json:"country"`                                 // Mobility Database field 'location.country_code'
	LocationMunicipality *string  `db:"municipality" json:"municipality"`                       // Mobility Database field 'location.municipality'
	LocationBbox         *string  `db:"bbox" json:"bbox"`                                       // Mobility Database bounding box
	LocationBboxKm       *float64 `db:"km" json:"km"`                                           // Mobility Database bounding box calculated area (KM)
}

// SourcesetOpResult represents the result of a Compute, Purge, or Load request
// exposing whether the operation was successful and the effected mdbids.
type SourcesetOpResult struct {
	Operation string `json:"operation"` // Name of operation (compute/purge/load)
	Success   bool   `json:"success"`   // Whether the operation was successful
	Mdbids    []int  `json:"mdbids"`    // Array of mdbids matched / performed against
}
